use actix_session::Session;
use actix_web::{web,HttpResponse};
use crate::dao::{
    services::user_service::*,
    domain::user::UserDao,
    traits::*
};
use crate::prelude::*;
async fn get_one_user(path:web::Path<(i64,)>) -> AppResult<HttpResponse> {
    let id = path.into_inner().0;
    Ok(HttpResponse::Ok().json(find_one_user_by_id(id, crate::get_db_pool!()).await?))
}
async fn get_self(session:Session) -> AppResult<HttpResponse> {
    let id:i64 = session.get(UserDao::get_session_key())?.ok_or(AppError::Unauthorized("".into()))?;
    Ok(HttpResponse::Ok().json(find_one_user_by_id(id, crate::get_db_pool!()).await?))
}
pub fn config_self_controller(cfg:&mut web::ServiceConfig) {
    cfg.service(web::resource("/").route(web::get().to(get_self)));
}
pub fn config_user_controller(cfg:&mut web::ServiceConfig) {
    cfg.service(web::resource("/list/{id}/").route(web::get().to(get_one_user)))
    .service(web::scope("/self").configure(config_self_controller));

}